<?php

namespace Home\Service;

class UserService extends CommonService {

    public function login($user) {
        $User = $this->getM();

        if (!$this->existUser($user['username'])) {
            return $this->errorResultReturn('用户不存在');
        }

        $account = $User->getByUsername($user['username']);
        // 密码验证
        if ($account['password'] != md5($user['password'])) {
            return $this->errorResultReturn('密码不正确！');
        }

        $_SESSION['sid'] = session_id();
        $_SESSION['user'] = $account;
        return $this->resultReturn(true);
    }

    public function logout() {
        session_destroy();
    }

    public function existUser($username) {
        if ($this->getM()->where("username='{$username}'")->count() > 0) {
            return true;
        }

        return false;
    }

    protected function getModelName() {
        return 'User';
    }
}
